/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-11-02
 * Time: 16:33
 */
public class TestH13 {
    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        for(int i = 0; i < n; i++) {
            int rest = 0;
            int step = 0;
            for(; step < n; step++) {
                int index = (i+step) % n;
                rest = rest + gas[index] - cost[index];
                if(rest < 0) {
                    break;
                }
            }
            if(rest >= 0) {
                return i;
            }
            i += step;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] gas = {1,2,3,4,5};
        int[] cost = {3,4,5,1,2};
        int ret = canCompleteCircuit(gas,cost);
        System.out.println(ret);
    }
}
